/**
 * Created by Administrator on 2014/12/17.
 */
var Fanli = (function(){
	var win, body;
	var hasInit = false;
	var winH = winW = boxW = boxH = 0;
	var animating = false;
	var popBoxHtml = [
		'<div class="fanliPopMain">',
			'<h3 id="fanli-tith" class="tith">测试分销商测试分销商测试分销商测试分销商</h3>',
			'<div class="fanliPopCon">',
				'<ul class="flistUl">',
					'<li class="fitem"><span class="t man">满</span><input id="man1" class="fgInp man" type="text"/><span class="t wan">万元</span><span class="t fan">返</span><input id="fan1" class="fgInp fan" type="text"/><span class="t per">%</span><span class="error"></span></li>',
					'<li class="fitem"><span class="t man">满</span><input id="man2" class="fgInp man" type="text"/><span class="t wan">万元</span><span class="t fan">返</span><input id="fan2" class="fgInp fan" type="text"/><span class="t per">%</span><span class="error"></span></li>',
					'<li class="fitem"><span class="t man">满</span><input id="man3" class="fgInp man" type="text"/><span class="t wan">万元</span><span class="t fan">返</span><input id="fan3" class="fgInp fan" type="text"/><span class="t per">%</span><span class="error"></span></li>',
					'<li class="fitem"><span class="t man">满</span><input id="man4" class="fgInp man" type="text"/><span class="t wan">万元</span><span class="t fan">返</span><input id="fan4" class="fgInp fan" type="text"/><span class="t per">%</span><span class="error"></span></li>',
				'</ul>',
				'<div class="fbtns"><span id="fanliCannelBtn" class="fbtn cannel">取消</span><span id="fanliCentainBtn" class="fbtn centain">确定并保存</span></div>',
			'</div>',
			'<a id="closeFanliBtn" class="closeFanliBtn">X</a>',
		'</div>'
	].join("");
	var verd = function(inpValue){
		var msg = "";
		if(!inpValue) return msg = "不能为空";
		if(!isNaN(inpValue) || inpValue<=0) return msg = "输入数值必须大于0，可以包含小数点";
	};
	function open(title,config){
		if(animating) return false;
		config = config || {};
		win = $(window);
		body = $("body");
		winH = winH || win.height();
		winW = winW || win.width();
		title = title || "title缺省"
		var opacity = typeof config.opacity !== "undefined" ? config.opacity : 0.5;
		boxW = config.width || 600;
		boxH = config.height || 400;
		var posX = (winW-boxW) / 2;
		var posY = (winH-boxH) / 2;
		var mask = $("#fnaliMask").length ? $("#fnaliMask") : $('<div id="fnaliMask" class="fnaliMask">').css({
			opacity : opacity,
			height : winH
		}).appendTo(body);
		var popBox = $("#fanliPopBox").length ? $("#fanliPopBox") : $('<div id="fanliPopBox" class="fanliPopBox">').css({
			width : boxW,
			height : boxH,
			left : posX,
			top : -boxH
		}).appendTo(body).html(popBoxHtml).hide();
		mask.fadeIn();
		popBox.show().find("#fanli-tith").text(title).end().find(".fgInp").val("").end().animate({top:posY});
		if(!hasInit){
			hasInit = true;
			init();
		}
	}
	function boxGoback(){
		animating = true;
		$("#fanliPopBox").animate({top:-(boxH+30)},function(){
			$("#fanliPopBox").hide();
			$("#fanliPopBox").find("span.error").hide();
			$("#fanliPopBox").find("input.error").removeClass("error");
			$("#fnaliMask").fadeOut();
			animating = false;
		})
	}
	function verdaor(){
		var inps = $("#fanliPopBox .fgInp[value!='']");
		var allowSubmit = true;
		if(!inps.length){
			allowSubmit = false;
			alert("请输入满返额度");
		}else{
			inps.each(function(index,inp){
				inp = $(inp);
				var value = inp.val();
				var errorTip = inp.parents("li").find(".error");
				var sibInp = inp.siblings(".fgInp");
				var sibInpValue = sibInp.val();
				if(isNaN(value) || value <= 0){
					allowSubmit = false;
					inp.addClass("error");
					errorTip.show().text("输入数值必须大于0，可以包含小数点");
				}
				if(!sibInpValue || isNaN(sibInpValue) || sibInpValue <= 0){
					allowSubmit = false;
					sibInp.addClass("error");
					errorTip.show().text("输入数值必须大于0，可以包含小数点");
				}
			})
		}
		if(allowSubmit){
			submit();
		}
	}
	function submit(){
		var data = {};
        var str =zax =  [];
        for(var i=1;i<5;i++){
            var ma = [];
            ma = "man";
            ma = ma+i;           
            if($("#"+ma).val()){
                var fa = [];
                fa = "fan";
                fa += i;
                data[$("#"+ma).val()] = $("#"+fa).val();
            }
        }
        /*
		data[$("#man1").val()] = $("#fan1").val();
        data[$("#man2").val()] = $("#fan2").val();
        data[$("#man3").val()] = $("#fan3").val();
        data[$("#man4").val()] = $("#fan4").val();
        */
        $.ajax({
        url:"../yd/call/commission.php",
        data: {
                    fid:$("#fanli-tith").text(),
                    method:"update",
                    man_fan:data
                },
        type:"POST",
        dataType:"JSON",
        success:function(res){
                if(res.code==200){
                    alert("配置成功");
                }
        },
        })
	}
	function init(){
		$("#closeFanliBtn").on("click",boxGoback);
		$("#fanliCentainBtn").on("click",verdaor);
		$("#fanliCannelBtn").on("click",boxGoback);
		$("#fanliPopBox").on("focus",".fgInp",function(e){
			var target = $(e.target);
			target.removeClass("error");
			target.parents(".fitem").children("span.error").hide();
		})
	}

	return {
		open : open,
		submit : submit
	}

})();

